/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

package scenic;

import java.awt.Rectangle;
import java.awt.geom.AffineTransform;

import scenic.jni.Renderer;

/**
 * <p>This scene node clips its children inside a clip area. The clip
 * area is defined by a scene. The clipping is performed by modulating
 * the child scene with the clip scene. The clip scene is usually a 
 * shape, but it can be any scene.</p>
 * 
 * <p>SceneClip can be used to paint a shape with a brush by setting
 * the shape as the clip shape and adding the brush as a child.</p>
 *
 */
public class SceneClip extends SceneContainer {
	private SceneNode clip;

	/**
	 * Construct an SceneClip object with the given clip scene.
	 * 
	 * @param clip the clip scene.
	 */
	public SceneClip(SceneNode clip) {
		this.clip = clip;
		if(isVisible())
			clip.addVisibleParent(this);
	}

	/**
	 * Gets the clip scene.
	 * 
	 * @return the clip scene.
	 */
	public SceneNode getClip() {
		return clip;
	}

	/**
	 * Sets the clip scene.
	 * 
	 * @param clip the clip scene.
	 */
	public void setClip(SceneNode clip) {
		if(isVisible())
			this.clip.removeVisibleParent(this);
		this.clip = clip;
		if(isVisible())
			this.clip.addVisibleParent(this);
		changed();
	}
	
	void draw(DrawContext context, AffineTransform transform, Rectangle visibleArea) {
		Rectangle bounds = getBounds(context, transform);
		int clipDrawType = clip.getDrawType(context, transform);
			
		if(bounds.width <= 0 || bounds.height <= 0)
			return;
		bounds = addSafetyMargin(bounds);
		if(context.renderer.beginSurface(context.context, bounds, Renderer.SURFACE_TYPE_COLOR) == 0)
			return;
		super.draw(context, transform, bounds);
		if(context.renderer.beginSurface(context.context, bounds, getSurfaceType(clipDrawType)) == 0)
			return;
		clip.draw(new DrawContext(context, clipDrawType), transform, bounds);
		context.renderer.color(context.context, 1.0f, 1.0f, 1.0f, 1.0f);
		context.renderer.drawSurfaceAndClip(context.context);
	}
	
	protected void show() {
		super.show();
		if(clip != null)
			clip.addVisibleParent(this);
	}
	
	protected void hide() {
		super.hide();
		if(clip != null)
			clip.removeVisibleParent(this);
	}
	
	Rectangle getBounds(DrawContext context, AffineTransform transform) {
		Rectangle result = super.getBounds(context, transform);

		Util.common(result, clip.getBounds(context, transform));
		return result;
	}
	
}
